﻿

Public Class frmOptionNew
    Dim mBackColor As Color

#Region "读取与保存配置"
    Dim CurrentOpt As New Db_CurrentOption.CurrentOption

    ''' <summary>
    ''' 初始化显示
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub frmOptions_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GetCurrentProject()
        If CurrentOpt.CurrentProject.Length = 0 Then
            MsgBox("未设置当前项目！")
            Me.Close()
        End If
        lblBMSet.Text = CurrentOpt.CurrentProject
        lblReadUnitSet.Text = CurrentOpt.CurrentProject
        lblXcSet.Text = CurrentOpt.CurrentProject

        mBackColor = txtBmLvl.BackColor
        InputPanel2.Enabled = True

        Dim mDb As New Db_Project
        Dim mOpt As New Db_Project.ProjectData
        mDb.Read(CurrentOpt.CurrentProject, mOpt)

        SetTextBoxIniValue(mOpt)

    End Sub
    Private Sub GetCurrentProject()
        Dim mDB As New Db_CurrentOption
        CurrentOpt = mDB.Read()
    End Sub
    Private Sub SetTextBoxIniValue(ByVal mOpt As Db_Project.ProjectData)
        With mOpt
            '基准点
            Dim Bms As List(Of LvlPoint) = mOpt.BMs
            If Not Bms Is Nothing Then
                lstBms.Items.Clear()
                lstBms.Items.Add("已有基准点:")
                For Each spnt As LvlPoint In Bms
                    lstBms.Items.Add(spnt.Name & ":" & spnt.Level.ToString("0.00000"))
                Next
            Else
                lstBms.Items.Add("已有基准点:")
            End If

            '中丝读数
            SetNoNullExam(txtOptMid, .MidReadUnit)

            '基辅读数
            SetNoNullExam(txtOptJiFu, .JiFuCha)

            '视距测量方式
            If mOpt.DistReadMod = "1" Then
                '直接读取视距
                rbDstD.Checked = True
                SetNoNullExam(txtOptDst2, .DistReadUnit)
            Else
                '测量上下丝换算视距
                rbDstUpDn.Checked = True
                SetNoNullExam(txtOptDst1, .UpDownReadUnit)
                SetNoNullExam(txtOptDst2, .DistMult)
            End If

            Dim mDB As New Db_XianCha
            Dim sTmp As String = mDB.Read_XC_Grade()
            Dim sLines() As String = sTmp.Split(vbCrLf)
            cboXCgrade.Items.Clear()
            For Each lv As String In sLines
                If lv.Trim.Length > 0 Then
                    cboXCgrade.Items.Add(lv.Trim)
                End If
            Next

            If .XC_Grade Is Nothing Then
                cboXCgrade.SelectedIndex = 0
                .XC_Grade = cboXCgrade.Text

            ElseIf .XC_Grade.Trim.Length = 0 Then
                cboXCgrade.SelectedIndex = 0
                .XC_Grade = cboXCgrade.ValueMember
            Else
                cboXCgrade.Text = .XC_Grade
            End If
        End With
    End Sub
    Private Sub SetNoNullExam(ByRef txt As TextBox, ByVal ExamStr As String)
        If Not ExamStr Is Nothing Then
            If CDbl(ExamStr) > 0 Then
                If txt.Name = "txtOptMid" Or txt.Name = "txtOptDst1" Then
                    Dim sR As Double = CDbl(ExamStr) * 1000
                    ExamStr = sR.ToString
                End If
                txt.Text = ExamStr.Trim
            End If
        End If
    End Sub

    ''' <summary>
    ''' 返回前保存
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub frmOption_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        UpdateReg()
        If cboXCgrade.Text = "自定义" Then
            SaveDefXC()
        End If
    End Sub

    Private Function UpdateReg() As Boolean
        Dim mDB As New Db_Project
        Dim mProject As New Db_Project.ProjectData

        '基准点
        If lstBms.Items.Count = 0 Then
            TabControl1.SelectedIndex = 0
            Return False
        End If
        Dim BMs As String = String.Empty
        For I As Integer = 1 To lstBms.Items.Count - 1
            Dim sLine As String = lstBms.Items(I)
            If sLine.Trim.Length > 0 And Math.Max(sLine.IndexOf(","), sLine.IndexOf(":")) > 0 Then
                If BMs = String.Empty Then
                    '第一个基准点
                    BMs = sLine.Trim
                Else
                    '后续基准点
                    BMs &= vbCrLf & sLine.Trim
                End If
            End If
        Next

        mProject.BMs = mDB.setBMsByString(BMs)

        '中丝读数
        If ExamTextBoxFoundErr(txtOptMid, True, 2, mProject.MidReadUnit) Then Return False

        '基辅读数
        If ExamTextBoxFoundErr(txtOptJiFu, True, 2, mProject.JiFuCha) Then Return False

        '视距测量方式
        If rbDstUpDn.Checked Then
            mProject.DistReadMod = "0"
            If ExamTextBoxFoundErr(txtOptDst1, True, 2, mProject.UpDownReadUnit) Then Return False
            If ExamTextBoxFoundErr(txtOptDst2, True, 2, mProject.DistMult) Then Return False
        Else
            mProject.DistReadMod = "1"
            If ExamTextBoxFoundErr(txtOptDst2, True, 2, mProject.DistReadUnit) Then Return False
        End If

        mProject.XC_Grade = cboXCgrade.Text

        mDB.SaveProjectOpt(CurrentOpt.CurrentProject, mProject)



        ''Dim mXC As New Gcs_WM_Tools.Db_XianCha.XianCha

        ''两次读数差
        'If ExamTextBoxFoundErr(txtXC_TwiceRead, True, 3, lvlRegistry.OptionDef.XC_TwiceRead) Then Return False

        ''基辅测高差之差
        'If ExamTextBoxFoundErr(txtXC_JiFuHeightErr, True, 3, lvlRegistry.OptionDef.XC_JiFuHeightErr) Then Return False

        ''最小读数
        'If ExamTextBoxFoundErr(txtXC_MinRead, True, 3, lvlRegistry.OptionDef.XC_MinRead) Then Return False

        ''视线长
        'If ExamTextBoxFoundErr(txtXC_Distace, True, 3, lvlRegistry.OptionDef.XC_Distace) Then Return False

        ''前后视距差
        'If ExamTextBoxFoundErr(txtXC_BackForeDistanceGap, True, 3, lvlRegistry.OptionDef.XC_BackForeDistanceGap) Then Return False

        ''累积视距差
        'If ExamTextBoxFoundErr(txtXC_BackForeDistanceGapTotal, True, 3, lvlRegistry.OptionDef.XC_BackForeDistanceGapTotal) Then Return False

        'Dim reg As New lvlRegistry
        'reg.RecordOptions()

        'Return True
    End Function
    Private Function ExamTextBoxFoundErr(ByRef txt As TextBox, ByVal NumExam As Boolean, ByVal FocusWhileErr As Integer, ByRef SetTo As String) As Boolean
        If txt.TextLength > 0 Then
            If NumExam Then
                If IsNumeric(txt.Text) Then
                    SetTo = txt.Text
                    If txt.Name = "txtOptMid" Or txt.Name = "txtOptDst1" Or txt.Name = "txtXC_TwiceRead" Or txt.Name = "txtXC_JiFuHeightErr" Then
                        SetTo = (CDbl(SetTo) / 1000).ToString("0.00000")
                    End If

                    Return False
                End If
            Else
                SetTo = txt.Text
                Return False
            End If
        End If

        TabControl1.SelectedIndex = FocusWhileErr
        txt.Focus()
        Return True

    End Function

#End Region


#Region "基准点录入"


    '基准点高程只接受数字输入
    Private Sub txtBMLvl_KeyPress(ByVal sender As Object, _
                                     ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                                     Handles txtBmLvl.KeyPress
        NumericTextboxExam(txtBmLvl.Text, e)
    End Sub

    Private Sub bBm_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bBm_Add.Click
        If txtBmNew.Text.Length > 0 And txtBmLvl.Text.Length > 0 Then
            Dim BmName As String = txtBmNew.Text.Trim
            Dim BmLvl As String = txtBmLvl.Text

            For I As Integer = 1 To lstBms.Items.Count - 1
                Dim item As String = lstBms.Items(I)
                Dim itmName As String = String.Empty, itmLevel As String = String.Empty
                StandardBM(item, itmName, itmLevel)

                If itmName = BmName Then
                    lstBms.Items(I) = BmName & ":" & CDbl(BmLvl).ToString("0.00000")
                    Exit Sub
                End If
            Next
            Me.lstBms.Items.Add(BmName & ":" & CDbl(BmLvl).ToString("0.00000"))
        End If
    End Sub

    Private Sub StandardBM(ByVal Value As String, ByRef bmName As String, ByRef bmLevel As String)
        Dim SV() As String
        If Value.IndexOf(":") > 0 Then
            SV = Value.Trim.Split(":")
        Else
            SV = Value.Trim.Split(",")
        End If

        If SV.Length = 2 And IsNumeric(SV(1)) Then
            bmName = SV(0)
            bmLevel = CDbl(SV(1)).ToString("0.00000")
        End If
    End Sub


    Private Sub bBm_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bBm_Del.Click
        If lstBms.SelectedIndex > 0 Then
            Me.lstBms.Items.Remove(lstBms.SelectedItem)
        End If
    End Sub

    Private Sub lstBm_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstBms.SelectedIndexChanged
        If lstBms.SelectedIndex > 0 Then
            Dim SV() As String = lstBms.Text.Split(":")
            If SV.Length = 2 Then
                txtBmNew.Text = SV(0)
                txtBmLvl.Text = SV(1)
                txtBmLvl.Focus()
            End If
        End If
    End Sub



#End Region

#Region "读数单位设置"

    Private Sub rbDstUpDn_CheckedChanged(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles rbDstUpDn.CheckedChanged, rbDstUpDn.CheckedChanged
        If rbDstD.Checked Then
            '直接读视距
            lblDstNote1.Visible = False
            txtOptDst1.Visible = False
            lblDstNote2.Text = "视距读数单位"

        ElseIf rbDstUpDn.Checked Then
            '读上下丝求视距
            lblDstNote1.Visible = True
            txtOptDst1.Visible = True
            lblDstNote2.Text = "视距乘常数"

        End If
    End Sub

    Private Sub txtOptMid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles txtOptMid.KeyPress, txtOptDst1.KeyPress, txtOptDst2.KeyPress, txtOptJiFu.KeyPress

        Dim txt As TextBox = CType(sender, TextBox)
        NumericTextboxExam(txt.Text, e)
    End Sub

#End Region

#Region "限差设置"
    ''' <summary>
    ''' 根据选项显示限差
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cboXcGrade_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles cboXCgrade.SelectedIndexChanged
        Dim mDb As New Db_XianCha
        Dim mXC As New Db_XianCha.XianCha
        mDb.Read(cboXCgrade.Text, mXC)
        txtXC_Distace.Text = mXC.Distance.ToString
        txtXC_TwiceRead.Text = mXC.TwoReadsErr * 1000
        txtXC_MinRead.Text = mXC.MinRead
        txtXC_JiFuHeightErr.Text = mXC.HeightInTwoReadsErr * 1000
        txtXC_BackForeDistanceGap.Text = mXC.StnDistErr
        txtXC_BackForeDistanceGapTotal.Text = mXC.TotalDistErr

        If cboXCgrade.Text = "自定义" Then
            txtXC_BackForeDistanceGap.Enabled = True
            txtXC_BackForeDistanceGapTotal.Enabled = True
            txtXC_Distace.Enabled = True
            txtXC_JiFuHeightErr.Enabled = True
            txtXC_MinRead.Enabled = True
            txtXC_TwiceRead.Enabled = True
            bSaveDefXC.Visible = True
        Else
            txtXC_BackForeDistanceGap.Enabled = False
            txtXC_BackForeDistanceGapTotal.Enabled = False
            txtXC_Distace.Enabled = False
            txtXC_JiFuHeightErr.Enabled = False
            txtXC_MinRead.Enabled = False
            txtXC_TwiceRead.Enabled = False
            bSaveDefXC.Visible = False
        End If
    End Sub


    ''' <summary>
    ''' 自定义限差只接受数字输入
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtXC_BackForeDistanceGap_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
    Handles txtXC_BackForeDistanceGap.KeyPress, txtXC_BackForeDistanceGapTotal.KeyPress, _
    txtXC_Distace.KeyPress, txtXC_JiFuHeightErr.KeyPress, txtXC_MinRead.KeyPress, txtXC_TwiceRead.KeyPress
        Dim txt As TextBox = CType(sender, TextBox)
        NumericTextboxExam(txt.Text, e)
    End Sub

    ''' <summary>
    ''' 保存自定义限差
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub bSaveDefXC_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles bSaveDefXC.Click
        SaveDefXC()
    End Sub
    Private Sub SaveDefXC()
        Try
            Dim mXC As New Db_XianCha.XianCha
            mXC.Distance = CDbl(txtXC_Distace.Text)
            mXC.HeightInTwoReadsErr = CDbl(txtXC_JiFuHeightErr.Text) / 1000
            mXC.MinRead = CDbl(txtXC_MinRead.Text)
            mXC.StnDistErr = CDbl(txtXC_BackForeDistanceGap.Text)
            mXC.TotalDistErr = CDbl(txtXC_BackForeDistanceGapTotal.Text)
            mXC.TwoReadsErr = CDbl(txtXC_TwiceRead.Text) / 1000
            mXC.Grade = "自定义"

            Dim mDB As New Db_XianCha
            mDB.RecordRow(mXC)

        Catch ex As Exception
            MsgBox("自定限差出错！")
            TabControl1.SelectedIndex = 2
        End Try
    End Sub




#End Region

    ''' <summary>
    ''' 检测只接受数字输入
    ''' </summary>
    ''' <param name="txtExist"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    ''' 
    Private Sub NumericTextboxExam(ByVal txtExist As String, ByRef e As System.Windows.Forms.KeyPressEventArgs)
        Select Case Asc(e.KeyChar)
            Case 32, 8, 48 To 57 '空格,退格,0-9不处理
            Case 46 '小数点，只能一个
                If InStr(txtExist, ".") <> 0 Then
                    e.Handled = True
                End If
            Case Else '其它的终止事件
                e.Handled = True
        End Select
    End Sub





End Class